package AST;
import java.util.ArrayList;
import java.util.Hashtable;

import regexInput.NFABuilder;


public class ASTReader {
	
	
	
	//private ArrayList<ASTNode> tree;
	private IDList IDs;
	
	public ASTReader(){
		//this.tree = tree;
		IDs = new IDList();
	}
	
	public Object evaluateTree(ASTNode tree){
		Object o = "";
		if (tree.getType().equals("Begin")){
			for(int i = 0; i < tree.getChildren().size();i++){
				evaluateTree(tree.getChildren().get(i));
			}
		}
		else if (tree.getType().equals("assign")){
			System.out.println("Assign: " + tree.getData() + "(");
			Object value = evaluateTree(tree.getChildren().get(0));
			IDs.assign(new ID(tree.getData(),value));
			System.out.println(")");
			return value;
		}
		else if (tree.getType().equals("operation")){
			ArrayList param1 = (ArrayList) evaluateTree(tree.getChildren().get(0));
			ArrayList param2 = (ArrayList) evaluateTree(tree.getChildren().get(1));
			System.out.println(param1);
			System.out.println(param2);
			
			if (tree.getData().equals("intersec")){
				System.out.println("intersect");
				return intersect(param1,param2);
			}
			else if (tree.getData().equals("diff")){
				System.out.println("difference");
				return difference(param1,param2);
			}
			else if (tree.getData().equals("union")){
				System.out.println("union");
				return union(param1,param2);
			}
		}
		else if (tree.getType().equals("#")){
			if (IDs.getID(tree.getData()) != null){
				System.out.println("#" + tree.getData());
				Object data = IDs.getID(tree.getData());
				return stringCounter((ArrayList)data);
			}
		}
		else if (tree.getType().equals("replace")){
			String param1 = tree.getChildren().get(0).getData();
			String param2 = tree.getChildren().get(1).getData();
			String param3 = tree.getChildren().get(2).getData();
			String param4 = tree.getChildren().get(3).getData();
			System.out.println("replace: " + param1 + ","+ param2 + ","+ param3 + ","+ param4);
			replace (param1,param2,param3,param4);
		}
		else if (tree.getType().equals("recurssivereplace")){
			String param1 = tree.getChildren().get(0).getData();
			String param2 = tree.getChildren().get(1).getData();
			String param3 = tree.getChildren().get(2).getData();
			String param4 = tree.getChildren().get(3).getData();
			System.out.println("recurssive replace: " + param1 + ","+ param2 + ","+ param3 + ","+ param4);
			replace (param1,param2,param3,param4);
		}
		else if (tree.getType().equals("print")){
			System.out.println("print");
			print(tree.getChildren().get(0).getData());
		}
		else if (tree.getType().equals("maxfreq")){
			System.out.println("Max frequency ("  + tree.getChildren().get(0).getData() + ")");
			String s = maxFrequency((ArrayList) IDs.getID(tree.getChildren().get(0).getData()));
			return s;
		}
		else if (tree.getType().equals("find")){
			String param1 = tree.getChildren().get(0).getData();
			String param2 = tree.getChildren().get(1).getData();
			System.out.println("find: " + param1 +"," + param2);
			return find (param1,param2);
		}
		else if (tree.getType().equals("data")){
			return tree.getData();
		}
		
		return o;
	}

	
	public ArrayList union(ArrayList list1,ArrayList list2){
		ArrayList result = list1;
		for (int i = 0; i < list2.size();i++){
			list1.add(list2.get(i));
		}
		return result;
	}
	
	public ArrayList intersect(ArrayList list1,ArrayList list2){
		ArrayList result = new ArrayList();
		for (int i = 0; i < list1.size();i++){
			for (int j = 0;j<list2.size();j++){
				if (list1.get(i).equals(list2.get(j))){
					result.add(list2.get(j));
				}
			}
		}
		return result;
	}
	public ArrayList difference(ArrayList list1,ArrayList list2){
		boolean found = false;
		ArrayList result = new ArrayList();
		for (int i = 0; i < list1.size();i++){
			for (int j = 0;j<list2.size();i++){
				if (list1.get(i).equals(list2.get(j))){
					found = true;
				}
			}
			if (!found){
				result.add(list1.get(i));
				found = false;
			}
		}
		return result;
	}
	
	public ArrayList find (String regex,String filename){
		ArrayList x = new ArrayList();
		String[] s = {"Hey"};
		
		x.add("Item1");
		x.add("Item2");
		x.add("Item1");
		x.add("Item3");
		return x;
	}
	public int stringCounter(ArrayList list){
		return list.size();
	}
	public void replace(String param1,String param2,String file1,String file2){
		
	}
	public void print(String name){
		System.out.println(IDs.getID(name).toString());
	}
	public String maxFrequency(ArrayList<String> list){
		String current = "";
		int currentCount = 0;
		String mostFreq = "";
		int max = 0;
		for (int i = 0; i < list.size();i++){
			current = list.get(i);
			for (int j = i; j < list.size();j++){
				if(current.equals(list.get(j))){
					currentCount++;
					if (currentCount > max){
						max = currentCount;
						mostFreq = list.get(j);
					}
				}
			}
			currentCount = 0;
		}
		return mostFreq;
	}
	
	
}
